package com.strelitzia.controller;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.strelitzia.entity.DynamicEntity;
import com.strelitzia.exception.StrelitziaException;
import com.strelitzia.service.DynamicService;
import com.strelitzia.vo.DynamicForm;
import com.strelitzia.vo.ResultVo;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

/**
 * @create 2023/12/25 10:23
 */

@RestController
@RequestMapping("/dynamic")
public class DynamicController {
    @Autowired
    DynamicService dynamicService;

    @PostMapping()
    public ResultVo add(DynamicForm dynamicForm) {
        ResultVo resultVo = new ResultVo();
        try {
            dynamicService.add(dynamicForm);
            resultVo.setMsg("发布成功");
        } catch (Exception e) {
            resultVo.setCode(1);
            resultVo.setMsg("发布失败");
        }
        return resultVo;
    }

    @GetMapping("/list")
    public ResultVo list() {
        ResultVo resultVo = new ResultVo();
        try {
            resultVo.setData(dynamicService.getList());
        } catch (Exception e) {
            resultVo.setMsg("获取失败");
            resultVo.setCode(1);
        }
        return resultVo;
    }

    @GetMapping("/search/{keyword}")
    public ResultVo search(@PathVariable("keyword") String keyword) {
        ResultVo resultVo = new ResultVo();
        try {
            resultVo.setData(dynamicService.getListForSearch(keyword));
        } catch (Exception e) {
            resultVo.setMsg("获取失败");
            resultVo.setCode(1);
        }
        return resultVo;
    }

    @GetMapping("/list/{id}")
    public ResultVo list(@PathVariable("id") Integer id) {
        ResultVo resultVo = new ResultVo();
        try {
            resultVo.setData(dynamicService.getList(id));
        } catch (Exception e) {
            resultVo.setMsg("获取失败");
            resultVo.setCode(1);
        }
        return resultVo;
    }

    @GetMapping("/list/friend")
    public ResultVo fList() {
        ResultVo resultVo = new ResultVo();
        try {
            resultVo.setData(dynamicService.getListForFriend());
        } catch (Exception e) {
            resultVo.setMsg("获取失败");
            resultVo.setCode(1);
        }
        return resultVo;
    }

    @GetMapping("/list/my")
    public ResultVo myList() {
        ResultVo resultVo = new ResultVo();
        try {
            resultVo.setData(dynamicService.getListForMe());
        } catch (Exception e) {
            resultVo.setMsg("获取失败");
            resultVo.setCode(1);
        }
        return resultVo;
    }

    @GetMapping("/like/{did}")
    public ResultVo like(@PathVariable("did") Integer did) {
        ResultVo resultVo = new ResultVo();
        try {
            dynamicService.like(did);
        } catch (Exception e) {
            resultVo.setCode(1);
            if (e instanceof StrelitziaException) {
                resultVo.setMsg(e.getMessage());
            }else {
                resultVo.setMsg("服务器错误");
            }
        }
        return resultVo;
    }

    @GetMapping("/unlike/{did}")
    public ResultVo unlike(@PathVariable("did") Integer did) {
        ResultVo resultVo = new ResultVo();
        try {
            dynamicService.unlike(did);
        } catch (Exception e) {
            resultVo.setCode(1);
            if (e instanceof StrelitziaException) {
                resultVo.setMsg(e.getMessage());
            }else {
                resultVo.setMsg("服务器错误");
            }
        }
        return resultVo;
    }

    @GetMapping("/getDynamic/{did}")
    public ResultVo getDynamic(@PathVariable("did") Integer did) {
        ResultVo resultVo = new ResultVo();
        try {
            resultVo.setData(dynamicService.getDynamic(did));
        } catch (Exception e) {
            resultVo.setCode(1);
            if (e instanceof StrelitziaException) {
                resultVo.setMsg(e.getMessage());
            }else {
                resultVo.setMsg("服务器错误");
            }
        }
        return resultVo;
    }

    @GetMapping("/status/{id}/{status}")
    public void setStatus(@PathVariable("id") Integer id, @PathVariable("status") Integer status) {
        DynamicEntity byId = dynamicService.getById(id);
        byId.setStatus(status);
        dynamicService.updateById(byId);
    }

    @GetMapping("/del/{id}")
    public void del(@PathVariable("id") Integer id) {
        dynamicService.del(id);
    }

    @GetMapping("/count/{uid}")
    public Integer count(@PathVariable("uid") Integer uid) {
        return dynamicService.count(new QueryWrapper<DynamicEntity>().eq("uid", uid));
    }
}
